$(function () {
    initTreeTable();
});

function initTreeTable() {
    var settings = {
        url: ctx + "session/list",
        pageSize: 100,
        columns: [{
            field: 'username',
            title: $MB.i18n('monitor.name')
        }, {
            field: 'startTimestamp',
            align: 'center',
            width: '180px',
            title: $MB.i18n('monitor.logintime')
        }, {
            field: 'lastAccessTime',
            align: 'center',
            width: '180px',
            title: $MB.i18n('monitor.time')
        }, {
            field: 'host',
            align: 'center',
            title: $MB.i18n('monitor.ip')
        }, {
            field: 'location',
            title: $MB.i18n('monitor.addr')
        }, {
            field: 'status',
            align: 'center',
            width: '80px',
            title: $MB.i18n('monitor.status'),
            formatter: function (value, row, index) {
                if (value === '1') return '<span class="badge badge-success">' + $MB.i18n('monitor.status1') + '</span>';
                if (value === '0') return '<span class="badge badge-danger">' + $MB.i18n('monitor.status0') + '</span>';
            }
        }, {
            title: $MB.i18n('monitor.op'),
            align: 'center',
            width: '80px',
            formatter: function (value, row, index) {
                return "<a href='#' onclick='offline(\"" + row.id + "\",\"" + row.status + "\",\"" + row.username + "\")'>" + $MB.i18n('monitor.offline') + "</a>";
            }
        }]
    };

    $MB.initTable('onlineTable', settings);
}

function offline(id, status, username) {
    var tips_info1 = $MB.i18n('tips.monitor.msg'),
        tips_info2 = $MB.i18n('tips.monitor.msg2');
    if (status === "0") {
        $MB.n_warning(tips_info1);
        return;
    }
    if (username === userName) {
        location.href = ctx + 'logout';
    }
    $.get(ctx + "session/forceLogout", {"id": id}, function (r) {
        if (r.code === 0) {
            $MB.n_success(tips_info2);
            $MB.refreshTable('onlineTable');
        } else {
            $MB.n_danger(r.msg);
        }
    }, "json");
}